#include "basesettingpage.h"
#include "ui_basesettingpage.h"
#include "smallblock.h"
#include "../model/osdutilsmodel.h"
#include "../model/osdremoteservicemodel.h"
#include <QKeyEvent>
#include "../event/vedioevent.h"
#include "../event/docevent.h"
#include "basepageworker.h"
#include "../control/msgpagecontroller.h"
#include "../control/mainwindowcontroller.h"
#include "../control/keyboardcontroller.h"
#include "viewglobal.h"
#include <QGraphicsDropShadowEffect>
#include <QStringList>
#include <QtDebug>
#include "../osdglobal.h"
#include <QLineEdit>
#include <QTime>

#define OUTPUT_SHOW_NUMBER  "show_number"
#define OUTPUT_SHOW_LOCAL   "show_local"
#define INPUT_INTERFACE     "input_interface"
#define INPUT_VOICE         "input_voice"
#define OUTPUT_VOICE        "output_voice"
#define PLATFORM            "platform"
#define RUN_SERVER          "run_server"
#define SERVER_IP           "server_ip"
#define SERVER_PORT         "server_port"
#define SVC_IP              "svc_ip"
#define SVC_PORT            "svc_port"

BaseSettingPage::BaseSettingPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BaseSettingPage)
{
    m_inputvol = 0;
    m_outputvol = 0;
    m_platHideList.clear();
    m_runserverHideList.clear();
    m_svcIpHideList.clear();
    m_svcPortHideList.clear();
    m_indexStr.clear();

    m_vedioMode.clear();/* << "单屏自动" << "老师特写" << "学生特写";*/
    m_vmIndex = 0;
    m_docMode.clear(); /*<< "电脑课件" << "课堂作品" << "讲台板书";*/
    m_dmIndex = 0;

    ui->setupUi(this);
    initOutPut();
    initVoiceSetting();
    initRegisterSetting();
    initFocusOrder();

    ui->pbVedioSet->setChecked(true);
    ui->stackedWidget->setCurrentWidget(ui->vediosetting);
    ui->pbVedioSet->installEventFilter(this);
    ui->pbSoundSet->installEventFilter(this);
    ui->pbRegisterSet->installEventFilter(this);
    ui->page->installEventFilter(this);
    ui->doc_widget->installEventFilter(this);
    ui->pushButton->installEventFilter(this);

    worker = new BasePageWorker;
    connect(this, SIGNAL(updateStatus(int)), worker, SLOT(work(int)));

    connect(&OSDRemoteServiceModel::instance(),
            SIGNAL(notifyConfigInfo(ConfigInfo)),
            this, SLOT(configSlot(ConfigInfo)));

    connect(&OSDRemoteServiceModel::instance(),
            SIGNAL(notifyModifyConfig(BaseResp)),
            this, SLOT(modifyConfigResponseSlot(BaseResp)));

    connect(&OSDRemoteServiceModel::instance(),
            SIGNAL(notifyRegister(BaseResp)),
            this, SLOT(resgisterResponseSlot(BaseResp)));
}

BaseSettingPage::~BaseSettingPage()
{
    workThread.quit();
    workThread.wait();
    delete worker;
    delete ui;
}

void BaseSettingPage::configSlot(ConfigInfo rv)
{
    QString key = rv.getKey();
    QString value = rv.getValue();
    if (rv.getKey() == "conf_video_stream"
            || rv.getKey() == "conf_document_stream"
            || rv.getKey() == "audio_input_type")
    {
        value = rv.getValue()+"|"+rv.getRange();
    }
    updateConfig(key,value);
}

bool BaseSettingPage::isIndex()
{
    return (ui->stackedWidget->currentIndex() == 0);
}

void BaseSettingPage::setItemIndex(QString name, int index)
{
    SmallBlock* block = this->findChild<SmallBlock*>(name);
    if (block)
    {
        block->setOption(index);
    }
}

void BaseSettingPage::setItemVol(QString name, int vol)
{
    SmallBlock* block = this->findChild<SmallBlock*>(name);
    if (block)
    {
        block->setVol(vol);
    }
}

void BaseSettingPage::setItemText(QString name, QString txt)
{
    SmallBlock* block = this->findChild<SmallBlock*>(name);
    if (block)
    {
        block->setText(txt);
    }
}

void BaseSettingPage::updateConfig(QString key, QString value)
{
    //输出模式
    if (key == QString("dual_display"))
    {
        int index = (value == "false") ? 0:1;
        setItemIndex(OUTPUT_SHOW_NUMBER, index);

    }
    else if (key == QString("show_local_video"))
    {
        int index = (value == "false") ? 1:0;
        setItemIndex(OUTPUT_SHOW_LOCAL, index);
    }
    else if (key == QString("conf_video_stream"))
    {
        QStringList list = value.split("|");
        if (list.count() < 1)
        {
            ui->lbvediotip->setText("");
            return;
        }
        QString vi = list[0];
        list.removeAt(0);

        QHash<QString, int> hashMap;
        m_vedioIndexMap.clear();
        m_vedioMode.clear();
        hashMap.clear();
        int i = 0;
        foreach(QString item, list)
        {
            QStringList vs = item.split("@");
            if (vs.count() < 2)
            {
                continue;
            }
            m_vedioIndexMap.insert(i,vs[1]);
            hashMap.insert(vs[1],i);
            i += 1;
            m_vedioMode << vs[0];
        }
        m_vmIndex = hashMap.value(vi,0);
        ui->lbvediotip->setText(m_vedioMode.value(m_vmIndex));
    }
    else if (key == QString("conf_document_stream"))
    {
        QStringList list = value.split("|");
        if (list.count() < 1)
        {
            ui->lbdoctip->setText("");
            return;
        }
        QString vi = list[0];
        list.removeAt(0);

        QHash<QString, int> hashMap;
        m_docIndexMap.clear();
        m_docMode.clear();
        hashMap.clear();
        int i = 0;
        foreach(QString item, list)
        {
            QStringList vs = item.split("@");
            if (vs.count() < 2)
            {
                continue;
            }
            m_docIndexMap.insert(i,vs[1]);
            hashMap.insert(vs[1],i);
            i += 1;
            m_docMode << vs[0];
        }

        m_dmIndex = hashMap.value(vi,0);
        ui->lbdoctip->setText(m_docMode.value(m_dmIndex));
    }
    else if (key == QString("audio_input_type"))
    {
        QStringList list = value.split("|");
        if (list.count() < 1)
        {
            return;
        }
        QString vi = list[0];
        list.removeAt(0);

        QHash<QString, int> hashMap;
        hashMap.clear();
        int i = 0;
        QStringList typestr;
        foreach(QString item, list)
        {
            QStringList vs = item.split("@");
            if (vs.count() < 2)
            {
                continue;
            }
            m_indexStr.insert(i,vs[1]);
            hashMap.insert(vs[1],i);
            i += 1;
            typestr << vs[0];
        }
        int index = hashMap.value(vi,0);
        SmallBlock* block = this->findChild<SmallBlock*>(INPUT_INTERFACE);
        if (block)
        {
            block->setSelectionList(typestr,value);
        }

        setItemIndex(INPUT_INTERFACE, index);
    }
    else if (key == QString("audio_input_volume")
             )
    {
        m_inputvol = value.toInt();
        setItemVol(INPUT_VOICE,value.toInt());
    }
    else if (key == QString("audio_output_volume"))
    {
        m_outputvol = value.toInt();
        setItemVol(OUTPUT_VOICE,value.toInt());
    }
    else if (key == QString("enable_platform_server"))
    {
        int index = (value == "false") ? 1:0;
        setItemIndex(PLATFORM, index);
    }
    else if (key == QString("enable_operation_server"))
    {
        int index = (value == "false") ? 1:0;
        setItemIndex(RUN_SERVER, index);
    }
    else if (key == QString("svc_server_address"))
    {
        setItemText(SVC_IP, value);
    }
    else if (key == QString("svc_server_port"))
    {
        setItemText(SVC_PORT, value);
    }
    else if (key == QString("platform_server_address"))
    {
        setItemText(SERVER_IP, value);
    }
    else if (key == QString("platform_server_port"))
    {
        setItemText(SERVER_PORT, value);
    }
}

void BaseSettingPage::initFocusOrder()
{
    QList<QObject *> orderlist;
    orderlist.clear();
    orderlist<< NULL << NULL << ui->pbVedioSet << ui->doc_widget;
    m_ctrlOrder.insert(ui->page, orderlist);
}

bool BaseSettingPage::eventFilter(QObject *o, QEvent *e)
{
    if (ui->pbVedioSet == o && e->type() == QEvent::FocusIn)
    {
        MainWindowController::instance().changeBackground("1");
        ui->pbVedioSet->setChecked(true);
        ui->stackedWidget->setCurrentWidget(ui->vediosetting);
        emit updateStatus(0);
    } else if (ui->pbSoundSet == o && e->type() == QEvent::FocusIn)
    {
        MainWindowController::instance().changeBackground("0");
        ui->pbSoundSet->setChecked(true);
        ui->stackedWidget->setCurrentWidget(ui->soundsetting);
        emit updateStatus(1);

    } else if (ui->pbRegisterSet == o && e->type() == QEvent::FocusIn)
    {
        MainWindowController::instance().changeBackground("0");
        ui->pbRegisterSet->setChecked(true);
        ui->stackedWidget->setCurrentWidget(ui->registersetting);
        emit updateStatus(2);
    }
    else if (e->type() == QEvent::KeyPress)
    {
        return true;
    }
    else if (e->type() == QEvent::KeyRelease)
    {
        QKeyEvent *ke = dynamic_cast<QKeyEvent*>(e);
        if (ke && (ke->key() == Qt::Key_Right
                   || ke->key() == Qt::Key_Down
                   || ke->key() == Qt::Key_Left
                   || ke->key() == Qt::Key_Up))
        {
            if (!m_ctrlOrder.contains(o))
            {
                return false;
            }

            //处理左右音量键
            if ((ke->key() == Qt::Key_Right || ke->key() == Qt::Key_Left)
                    && (o == this->findChild<SmallBlock*>(INPUT_VOICE) || o == this->findChild<SmallBlock*>(OUTPUT_VOICE)))
            {
                return false;
            }

            if ((ke->key() == Qt::Key_Right || ke->key() == Qt::Key_Left)
             && (o == this->findChild<SmallBlock*>(SERVER_IP)
                || o == this->findChild<SmallBlock*>(SERVER_PORT)
                || o == this->findChild<SmallBlock*>(SVC_IP)
                || o == this->findChild<SmallBlock*>(SVC_PORT) ))
            {

                SmallBlock* s = dynamic_cast<SmallBlock*>(o);
                if (s->lineEdit()->hasFocus())
                {
                    int pos = s->getCursorPos();
                    if (pos >= 0 && pos <= (s->getText().length()))
                        return false;
                }
            }

            QList<QObject*> objs = m_ctrlOrder.value(o);
            //处理注册中隐藏控件的情况
            if (this->findChild<SmallBlock*>(SERVER_IP)->isHidden())
            {
                if (o == this->findChild<SmallBlock*>(PLATFORM)) {
                    objs = m_platHideList;
                } else if (o == this->findChild<SmallBlock*>(RUN_SERVER)) {
                    objs = m_runserverHideList;
                } else if (o == this->findChild<SmallBlock*>(SVC_IP)) {
                    objs = m_svcIpHideList;
                } else if (o == this->findChild<SmallBlock*>(SVC_PORT)) {
                    objs = m_svcPortHideList;
                }
            }else if (!this->findChild<SmallBlock*>(SERVER_IP)->isEnabled()
                      && !this->findChild<SmallBlock*>(SVC_IP)->isEnabled())
            {
                if (o == this->findChild<SmallBlock*>(PLATFORM)) {
                    objs.clear();
                    objs << NULL
                         << ui->pushButton << ui->pbRegisterSet << this->findChild<SmallBlock*>(RUN_SERVER);
                } else if (o == this->findChild<SmallBlock*>(RUN_SERVER)) {
                    objs.clear();
                    objs << NULL
                         << ui->pushButton << this->findChild<SmallBlock*>(PLATFORM) << NULL;
                } else if (o == ui->pushButton)
                {
                    objs.clear();
                    objs << this->findChild<SmallBlock*>(PLATFORM)
                         << NULL << ui->pbRegisterSet << NULL;
                }
            }else if (this->findChild<SmallBlock*>(SERVER_IP)->isEnabled()
                      && !this->findChild<SmallBlock*>(SVC_IP)->isEnabled())
            {
                if (o == this->findChild<SmallBlock*>(SERVER_IP)) {
                    objs.clear();
                    objs << this->findChild<SmallBlock*>(PLATFORM)
                         << ui->pushButton << ui->pbRegisterSet << this->findChild<SmallBlock*>(SERVER_PORT);
                } else if (o == this->findChild<SmallBlock*>(SERVER_PORT)) {
                    objs.clear();
                    objs << this->findChild<SmallBlock*>(RUN_SERVER)
                         << ui->pushButton << this->findChild<SmallBlock*>(SERVER_IP) << NULL;
                } else if (o == ui->pushButton)
                {
                    objs.clear();
                    objs << this->findChild<SmallBlock*>(SERVER_IP)
                         << NULL << ui->pbRegisterSet << NULL;
                }
            }

            QObject *target = objs.at(OSDUtilsModel::instance().keyIndex(ke->key()));
            if (target)
            {
                if (target == ui->page)
                {
                }
                QWidget* w = dynamic_cast<QWidget*>(target);
                if (w) w->setFocus();
            }
            return true;
        }else if (ke && ke->key() == Qt::Key_Escape)
        {
            MainWindowController::instance().changeSubPage(OSD::ViewGlobal::SUB_PAGE_INDEX);
            return true;
        }
    }else if (e->type() == VedioEvent::eventType)
    {
        qDebug() << "Vedio Change Event!";
        changeVedioOption();
        e->accept();
        return true;
    }else if (e->type() == DocEvent::eventType)
    {
        qDebug() << "Doc Change Event!";
        changeDocOption();
        e->accept();
        return true;
    }

    if (ui->pushButton == o)
    {
        if (e->type() == QEvent::FocusIn)
        {
            QGraphicsDropShadowEffect *pEffect = new QGraphicsDropShadowEffect(ui->pushButton);
            pEffect->setOffset(0, 0);
            pEffect->setColor(QColor(41, 163, 255, 255));
            pEffect->setBlurRadius(30);
            ui->pushButton->setGraphicsEffect(pEffect);
            return true;
        }
        else if (e->type() == QEvent::FocusOut)
        {
            ui->pushButton->setGraphicsEffect(NULL);
            return true;
        }else if (e->type() == QEvent::MouseButtonRelease)
        {
            on_pushButton_clicked();
        }
    }
    return false;
}

void BaseSettingPage::modifyConfigResponseSlot(BaseResp rv)
{
    if (rv.baseName() == QString("conf_video_stream"))
    {
        if (rv.getIsSuccess())
        {
            int nextIndex = (m_vmIndex+1) % m_vedioMode.count();
            m_vmIndex = nextIndex;
            ui->lbvediotip->setText(m_vedioMode.value(m_vmIndex));
        }
    }
    else if (rv.baseName() == QString("conf_document_stream"))
    {
        if (rv.getIsSuccess())
        {
            int nextIndex = (m_dmIndex+1) % m_docMode.count();
            m_dmIndex = nextIndex;
            ui->lbdoctip->setText(m_docMode.value(m_dmIndex));
        }
    }
    else if (rv.baseName() == QString("dual_display"))
    {
        if (rv.getIsSuccess())
        {
            SmallBlock* block = this->findChild<SmallBlock*>(OUTPUT_SHOW_NUMBER);
            block->changeOption();
        }
    }
    else if (rv.baseName() == QString("show_local_video"))
    {
        if (rv.getIsSuccess())
        {
            SmallBlock* block = this->findChild<SmallBlock*>(OUTPUT_SHOW_LOCAL);
            block->changeOption();
        }
    }
    else if (rv.baseName() == QString("audio_input_type"))
    {
        if (rv.getIsSuccess())
        {
            SmallBlock* block = this->findChild<SmallBlock*>(INPUT_INTERFACE);
            block->changeOption();
        }
    }
    else if (rv.baseName() == QString("audio_input_volume"))
    {
        if (rv.getIsSuccess())
        {
            setItemVol(INPUT_VOICE, m_inputvol);
        }
    }
    else if (rv.baseName() == QString("audio_output_volume"))
    {
        if (rv.getIsSuccess())
        {
            setItemVol(OUTPUT_VOICE, m_outputvol);
        }
    }
    else if (rv.baseName() == QString("enable_platform_server"))
    {
        if (rv.getIsSuccess())
        {
            SmallBlock* block = this->findChild<SmallBlock*>(PLATFORM);
            block->changeOption();
        }
    }
    else if (rv.baseName() == QString("enable_operation_server"))
    {
        if (rv.getIsSuccess())
        {
            SmallBlock* block = this->findChild<SmallBlock*>(RUN_SERVER);
            block->changeOption();
        }
    }
    else if (rv.baseName() == QString("platform_server_address")
           || rv.baseName() == QString("platform_server_port")
           || rv.baseName() == QString("svc_server_address")
           || rv.baseName() == QString("platform_server_address")
             )
    {
        if (!rv.getIsSuccess())
        {
            MsgPageController::instance().showMsg(rv.getCodeString());
        }
    }

}

void BaseSettingPage::changeVedioOption()
{
    if (m_vedioMode.count() == 0)
        return ;

    int nextIndex = (m_vmIndex+1) % m_vedioMode.count();

    QString key = "conf_video_stream";

    QString value = "";

    value = m_vedioIndexMap.value(nextIndex,"");
    OSDRemoteServiceModel::instance().modifyConfig(key, value);   //设置成功后，设置界面
}

void BaseSettingPage::changeDocOption()
{
    if (m_docMode.count() == 0)
        return ;
    int nextIndex = (m_dmIndex+1) % m_docMode.count();

    QString key = "conf_document_stream";

    QString value = "";
    value = m_docIndexMap.value(nextIndex,"");
    OSDRemoteServiceModel::instance().modifyConfig(key, value);   //设置成功后，设置界面
}

void BaseSettingPage::initOutPut()
{
    SmallBlock *show_block = new SmallBlock;
    show_block->setObjectName(OUTPUT_SHOW_NUMBER);
    show_block->setTitleLabel("输出模式");
    show_block->setMaximumSize(484,120);
    show_block->setMinimumSize(484,120);
    QStringList list;
    list << "单显" << "双显";
    show_block->setSelectionList(list,"单显");
    ui->outmode_layout->addWidget(show_block);
    show_block->installEventFilter(this);
    connect(show_block, SIGNAL(optionChangeSig(int)), this, SLOT(outputModeChanged(int)));

    SmallBlock *showlocal_block = new SmallBlock;
    showlocal_block->setObjectName(OUTPUT_SHOW_LOCAL);
    showlocal_block->setMaximumSize(484,120);
    showlocal_block->setMinimumSize(484,120);
    showlocal_block->setTitleLabel("显示本地");
    list.clear();
    list << "是" << "否";
    showlocal_block->setSelectionList(list,"是");
    ui->showlocal_layout->addWidget(showlocal_block);
    showlocal_block->installEventFilter(this);
    connect(showlocal_block, SIGNAL(optionChangeSig(int)), this, SLOT(showLocalChanged(int)));

    QList<QObject *> orderlist;
    orderlist.clear();
    orderlist<< NULL << ui->pbSoundSet << NULL << show_block;
    m_ctrlOrder.insert(ui->pbVedioSet, orderlist);

//    orderlist.clear();
//    orderlist<< NULL << NULL << ui->page << show_block;
//    m_ctrlOrder.insert(ui->doc_widget, orderlist);

    orderlist.clear();
    orderlist<< NULL << showlocal_block << ui->pbVedioSet << NULL;
    m_ctrlOrder.insert(show_block, orderlist);

    orderlist.clear();
    orderlist<< show_block << NULL << ui->pbVedioSet << NULL;
    m_ctrlOrder.insert(showlocal_block, orderlist);
}

void BaseSettingPage::initVoiceSetting()
{
    SmallBlock *input_block = new SmallBlock;
    input_block->setObjectName(INPUT_INTERFACE);
    input_block->setMaximumSize(600,120);
    input_block->setMinimumSize(600,120);
    input_block->setTitleLabel("输入接口");
    ui->input_layout->addWidget(input_block);
    input_block->installEventFilter(this);
    connect(input_block, SIGNAL(optionChangeSig(int)), this, SLOT(inputChanged(int)));

    SmallBlock *invoice_block = new SmallBlock(2);
    invoice_block->setObjectName(INPUT_VOICE);
    invoice_block->setMaximumSize(600,120);
    invoice_block->setMinimumSize(600,120);
    invoice_block->setTitleLabel("输入音量");
    ui->voice_layout->addWidget(invoice_block);
    invoice_block->installEventFilter(this);
    connect(invoice_block, SIGNAL(volChangedSig(int)), this, SLOT(inputVolChanged(int)));

    SmallBlock *outvoice_block = new SmallBlock(2);
    outvoice_block->setObjectName(OUTPUT_VOICE);
    outvoice_block->setMaximumSize(600,120);
    outvoice_block->setMinimumSize(600,120);
    outvoice_block->setTitleLabel("输出音量");
    ui->outvoice_layout->addWidget(outvoice_block);
    outvoice_block->installEventFilter(this);
    connect(outvoice_block, SIGNAL(volChangedSig(int)), this, SLOT(outputVolChanged(int)));

    QList<QObject *> orderlist;
    orderlist.clear();
    orderlist<< ui->pbVedioSet << ui->pbRegisterSet << NULL << input_block;
    m_ctrlOrder.insert(ui->pbSoundSet, orderlist);

    orderlist.clear();
    orderlist<< NULL << invoice_block << ui->pbSoundSet << NULL;
    m_ctrlOrder.insert(input_block, orderlist);

    orderlist.clear();
    orderlist<< input_block << outvoice_block << NULL << NULL;
    m_ctrlOrder.insert(invoice_block, orderlist);

    orderlist.clear();
    orderlist<< invoice_block << NULL << NULL << NULL;
    m_ctrlOrder.insert(outvoice_block, orderlist);
}

void BaseSettingPage::initRegisterSetting()
{
    SmallBlock *platform_block = new SmallBlock;
    platform_block->setObjectName(PLATFORM);
    platform_block->setMaximumSize(500,120);
    platform_block->setMinimumSize(500,120);
    platform_block->setParent(this);
    platform_block->setTitleLabel("业务平台");
    QStringList list;
    list << "启用" << "不启用";
    platform_block->setSelectionList(list,"启用");
    ui->platform_layout->addWidget(platform_block);
    platform_block->installEventFilter(this);
    connect(platform_block, SIGNAL(selctionChanged(QString)), this, SLOT(dealPlatform(QString)));
    connect(platform_block, SIGNAL(optionChangeSig(int)), this, SLOT(platformChanged(int)));


    SmallBlock *runserver_block = new SmallBlock;
    runserver_block->setObjectName(RUN_SERVER);
    runserver_block->setMaximumSize(500,120);
    runserver_block->setMinimumSize(500,120);
    runserver_block->setParent(this);
    runserver_block->setTitleLabel("部署方式");
    list.clear();
    list << "公有" << "私有";
    runserver_block->setSelectionList(list,"公有");
    ui->runserver_layout->addWidget(runserver_block);
    runserver_block->installEventFilter(this);
    connect(runserver_block, SIGNAL(selctionChanged(QString)), this, SLOT(dealRunServer(QString)));
    connect(runserver_block, SIGNAL(optionChangeSig(int)), this, SLOT(runserverChanged(int)));

    SmallBlock *serverip_block = new SmallBlock(0);
    serverip_block->setObjectName(SERVER_IP);
    serverip_block->setMaximumSize(500,120);
    serverip_block->setMinimumSize(500,120);
    serverip_block->setParent(this);
    serverip_block->setTitleLabel("业务平台服务地址");
    serverip_block->setText("");
    serverip_block->setLineEditValidator(STR_IP_REGEXP);
    serverip_block->setLineEditPlaceHolder("请输入业务平台服务地址");
    ui->serverip_layout->addWidget(serverip_block);
    serverip_block->installEventFilter(this);
    connect(serverip_block, SIGNAL(textStartSig()), this, SLOT(showKeyBoard()));
    connect(serverip_block, SIGNAL(textFinishedSig(QString)), this, SLOT(lineEditChanged(QString)));

    SmallBlock *serverport_block = new SmallBlock(0);
    serverport_block->setObjectName(SERVER_PORT);
    serverport_block->setMaximumSize(500,120);
    serverport_block->setMinimumSize(500,120);
    serverport_block->setParent(this);
    serverport_block->setTitleLabel("业务平台端口");
    serverport_block->setText("");
    serverport_block->setLineEditValidator(STR_PORT_REGEXP);
    serverport_block->setLineEditPlaceHolder("请输入业务平台服务端口");
    ui->serverport_layout->addWidget(serverport_block);
    serverport_block->installEventFilter(this);
    connect(serverport_block, SIGNAL(textStartSig()), this, SLOT(showKeyBoard()));
    connect(serverport_block, SIGNAL(textFinishedSig(QString)), this, SLOT(lineEditChanged(QString)));

    SmallBlock *svcip_block = new SmallBlock(0);
    svcip_block->setObjectName(SVC_IP);
    svcip_block->setMaximumSize(500,120);
    svcip_block->setMinimumSize(500,120);
    svcip_block->setParent(this);
    svcip_block->setTitleLabel("云视讯服务地址");
    svcip_block->setText("");
    svcip_block->setLineEditValidator(STR_IP_REGEXP);
    svcip_block->setLineEditPlaceHolder("请输入云视讯服务地址");
    ui->svcip_layout->addWidget(svcip_block);
    svcip_block->installEventFilter(this);
    connect(svcip_block, SIGNAL(textStartSig()), this, SLOT(showKeyBoard()));
    connect(svcip_block, SIGNAL(textFinishedSig(QString)), this, SLOT(lineEditChanged(QString)));

    SmallBlock *svcport_block = new SmallBlock(0);
    svcport_block->setObjectName(SVC_PORT);
    svcport_block->setMaximumSize(500,120);
    svcport_block->setMinimumSize(500,120);
    svcport_block->setParent(this);
    svcport_block->setTitleLabel("云视讯服务端口");
    svcport_block->setText("");
    svcport_block->setLineEditValidator(STR_PORT_REGEXP);
    svcport_block->setLineEditPlaceHolder("请输入云视讯端口服务端口");
    ui->svcport_layout->addWidget(svcport_block);
    svcport_block->installEventFilter(this);
    connect(svcport_block, SIGNAL(textStartSig()), this, SLOT(showKeyBoard()));
    connect(svcport_block, SIGNAL(textFinishedSig(QString)), this, SLOT(lineEditChanged(QString)));

    QList<QObject *> orderlist;
    orderlist.clear();
    orderlist<< ui->pbSoundSet << NULL << NULL << platform_block;
    m_ctrlOrder.insert(ui->pbRegisterSet, orderlist);

    orderlist.clear();
    orderlist<< NULL << serverip_block << ui->pbRegisterSet << runserver_block;
    m_ctrlOrder.insert(platform_block, orderlist);
    m_platHideList << NULL << svcip_block << ui->pbRegisterSet << NULL;


    orderlist.clear();
    orderlist<< NULL << serverport_block << platform_block << NULL;
    m_ctrlOrder.insert(runserver_block, orderlist);
    m_runserverHideList << NULL << svcport_block << platform_block << NULL;

    orderlist.clear();
    orderlist<< platform_block << svcip_block << ui->pbRegisterSet << serverport_block;
    m_ctrlOrder.insert(serverip_block, orderlist);

    orderlist.clear();
    orderlist<< runserver_block << svcport_block << serverip_block << NULL;
    m_ctrlOrder.insert(serverport_block, orderlist);

    orderlist.clear();
    orderlist<< serverip_block << ui->pushButton << ui->pbRegisterSet << svcport_block;
    m_ctrlOrder.insert(svcip_block, orderlist);
    m_svcIpHideList << platform_block << ui->pushButton << ui->pbRegisterSet << svcport_block;

    orderlist.clear();
    orderlist<< serverport_block << ui->pushButton << svcip_block << NULL;
    m_ctrlOrder.insert(svcport_block, orderlist);
    m_svcPortHideList << platform_block << ui->pushButton << svcip_block << NULL;

    orderlist.clear();
    orderlist<< svcip_block << NULL << ui->pbRegisterSet << NULL;
    m_ctrlOrder.insert(ui->pushButton, orderlist);
}

void BaseSettingPage::showKeyBoard()
{
    QObject *o = sender();
    QLineEdit *lineedit = dynamic_cast<SmallBlock *>(o)->lineEdit();
    KeyBoardController::instance().show(lineedit,0);
//    if (o == this->findChild<SmallBlock*>(NET_SETTING_MUSK)
//       || o == this->findChild<SmallBlock*>(NET_SETTING_GATEWAY))
//    {
//        QMargins margins = ui->verticalLayout_5->contentsMargins();
//        margins.setTop(0);
//        ui->verticalLayout_5->setContentsMargins(margins);
//    }
}

void BaseSettingPage::outputModeChanged(int next)
{
    QString key = "dual_display";
    OSDRemoteServiceModel::instance().modifyConfig(key, next == 0 ? "false":"true");
}

void BaseSettingPage::showLocalChanged(int next)
{
    QString key = "show_local_video";
    OSDRemoteServiceModel::instance().modifyConfig(key, next == 0 ? "true":"false");
}

void BaseSettingPage::inputChanged(int next)
{
    QString key = "audio_input_type";
    OSDRemoteServiceModel::instance().modifyConfig(key, m_indexStr.value(next,"0"));
}

void BaseSettingPage::inputVolChanged(int vol)
{
    QString key = "audio_input_volume";
    m_inputvol = vol;
    OSDRemoteServiceModel::instance().modifyConfig(key, QString::number(vol));
}

void BaseSettingPage::outputVolChanged(int vol)
{
    QString key = "audio_output_volume";
    m_outputvol = vol;
    OSDRemoteServiceModel::instance().modifyConfig(key, QString::number(vol));
}

void BaseSettingPage::platformChanged(int next)
{
    QString key = "enable_platform_server";
    OSDRemoteServiceModel::instance().modifyConfig(key, next == 0 ? "true":"false");
}

void BaseSettingPage::runserverChanged(int next)
{
    QString key = "enable_operation_server";
    OSDRemoteServiceModel::instance().modifyConfig(key, next == 0 ? "true":"false");
}

void BaseSettingPage::lineEditChanged(QString value)
{
    QObject *o = sender();
    SmallBlock* block = dynamic_cast<SmallBlock*>(o);

    if (block)
    {
        QString key = "";
        if (block->objectName() == QString(SERVER_IP))
        {
            key = "platform_server_address";
        }
        else if (block->objectName() == QString(SERVER_PORT))
        {
            key = "platform_server_port";
        }
        else if (block->objectName() == QString(SVC_IP))
        {
            key = "svc_server_address";
        }
        else if (block->objectName() == QString(SVC_PORT))
        {
            key = "svc_server_port";
        }

        if (!key.isEmpty())
        {
            OSDRemoteServiceModel::instance().modifyConfig(key, value);
        }
    }
}

void BaseSettingPage::dealPlatform(QString value)
{
    if (value == "启用")
    {
        if (this->findChild<SmallBlock*>(RUN_SERVER))
        {
            this->findChild<SmallBlock*>(RUN_SERVER)->setBlockEnable(true);
        }
        if (this->findChild<SmallBlock*>(SERVER_IP))
        {
            this->findChild<SmallBlock*>(SERVER_IP)->show();
        }
        if (this->findChild<SmallBlock*>(SERVER_PORT))
        {
            this->findChild<SmallBlock*>(SERVER_PORT)->show();
        }
        if (this->findChild<SmallBlock*>(SVC_IP))
        {
            this->findChild<SmallBlock*>(SVC_IP)->setBlockEnable(false);
        }
        if (this->findChild<SmallBlock*>(SVC_PORT))
        {
            this->findChild<SmallBlock*>(SVC_PORT)->setBlockEnable(false);
        }
    }
    else
    {
        if (this->findChild<SmallBlock*>(RUN_SERVER))
        {
            this->findChild<SmallBlock*>(RUN_SERVER)->setBlockEnable(false);
        }
        if (this->findChild<SmallBlock*>(SERVER_IP))
        {
            this->findChild<SmallBlock*>(SERVER_IP)->hide();
        }
        if (this->findChild<SmallBlock*>(SERVER_PORT))
        {
            this->findChild<SmallBlock*>(SERVER_PORT)->hide();
        }
        if (this->findChild<SmallBlock*>(SVC_IP))
        {
            this->findChild<SmallBlock*>(SVC_IP)->setBlockEnable(true);
        }
        if (this->findChild<SmallBlock*>(SVC_PORT))
        {
            this->findChild<SmallBlock*>(SVC_PORT)->setBlockEnable(true);
        }
    }
}

void BaseSettingPage::dealRunServer(QString value)
{
    if (value == "公有")
    {
        if (this->findChild<SmallBlock*>(SERVER_IP))
        {
            this->findChild<SmallBlock*>(SERVER_IP)->setBlockEnable(false);
        }
        if (this->findChild<SmallBlock*>(SERVER_PORT))
        {
            this->findChild<SmallBlock*>(SERVER_PORT)->setBlockEnable(false);
        }
        if (this->findChild<SmallBlock*>(SVC_IP))
        {
            this->findChild<SmallBlock*>(SVC_IP)->setBlockEnable(false);
        }
        if (this->findChild<SmallBlock*>(SVC_PORT))
        {
            this->findChild<SmallBlock*>(SVC_PORT)->setBlockEnable(false);
        }
    }
    else
    {
        if (this->findChild<SmallBlock*>(SERVER_IP))
        {
            this->findChild<SmallBlock*>(SERVER_IP)->setBlockEnable(true);
        }
        if (this->findChild<SmallBlock*>(SERVER_PORT))
        {
            this->findChild<SmallBlock*>(SERVER_PORT)->setBlockEnable(true);
        }

        if (this->findChild<SmallBlock*>(RUN_SERVER))
        {
            if (!this->findChild<SmallBlock*>(RUN_SERVER)->isEnabled())
                return;
        }

        if (this->findChild<SmallBlock*>(SVC_IP))
        {
            this->findChild<SmallBlock*>(SVC_IP)->setBlockEnable(false);
        }
        if (this->findChild<SmallBlock*>(SVC_PORT))
        {
            this->findChild<SmallBlock*>(SVC_PORT)->setBlockEnable(false);
        }
    }
}

void BaseSettingPage::on_pushButton_clicked()
{
    OSDRemoteServiceModel::instance().registerDevice();
}

void BaseSettingPage::resgisterResponseSlot(BaseResp rv)
{
    if (rv.getIsSuccess())
    {
        QTime n=QTime::currentTime();
        QTime now;
        do{
            now=QTime::currentTime();
        }   while(n.msecsTo(now)<=1000);
        MainWindowController::instance().updateStatus();
    }
    else
    {
        MsgPageController::instance().showMsg(rv.getCodeString());
    }
}

void BaseSettingPage::showEvent(QShowEvent *e)
{
    QWidget::showEvent(e);
    ui->pbVedioSet->setFocus();
}
